﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using PPFinance.Common;

namespace PPFinance.UI
{
    public partial class NavBar : UserControl
    {
        public delegate void NavbarHander(string menuText);
        public event NavbarHander OnClick;
        public NavBar()
        {
            InitializeComponent();
        }



        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            tbIEEx.Tag = true;
            tbSysEx.Tag = true;
            NavAm(grIE);
            NavAm(grSys);
        }

        void NavAm(Grid gr)
        {
            var tbIEs = gr.Children.Where(cl => cl.GetType() == typeof(TextBlock));
            foreach (var tb in tbIEs)
            {
                var tbInstall = tb as TextBlock;
                if (tbInstall != null)
                {
                    tbInstall.MouseEnter += new MouseEventHandler(tb_MouseEnter);
                    tbInstall.MouseLeave += new MouseEventHandler(tbInstall_MouseLeave);
                    tbInstall.MouseLeftButtonDown += (object sender, MouseButtonEventArgs e) => {
                        OnClick(tbInstall.Text);
                    };
                }
            }
        }

       

        void tbInstall_MouseLeave(object sender, MouseEventArgs e)
        {
            var tb = sender as TextBlock;
            ColorConverter cc = new ColorConverter();
            tb.Foreground = cc.Convert("#000000");
            tb.FontWeight = FontWeights.Normal;
        }

        void tb_MouseEnter(object sender, MouseEventArgs e)
        {
            var tb = sender as TextBlock;
            ColorConverter cc = new ColorConverter();
            tb.Foreground = cc.Convert("#FF0000");
            tb.FontWeight = FontWeights.Light;

        }

        private void IEGroup_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((bool)tbIEEx.Tag)
            {
                tbIEEx.Text = "▲";
                Play(grIE, 10, true, 0);
                tbIEEx.Tag = false;
            }
            else
            {
                tbIEEx.Text = "▼";
                Play(grIE, 10, false, (26 * 5) - 1);
                tbIEEx.Tag = true;
            }

        }

        private void sysGroup_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((bool)tbSysEx.Tag)
            {
                tbSysEx.Text = "▲";
                Play(grSys, 10, true, 0);
                tbSysEx.Tag = false;
            }
            else
            {
                tbSysEx.Text = "▼";
                Play(grSys, 10, false, (26 * 5) - 1);
                tbSysEx.Tag = true;
            }
        }


        void Play(Grid gr, int speed, bool expand, int end)
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += (object tsender, EventArgs te) =>
            {
                if (expand)
                {
                    if (gr.Height >= speed)
                    {
                        gr.Height = gr.Height - speed;
                    }
                    else
                    {
                        gr.Height = end;
                    }
                    if (gr.Height <= end)
                    {
                        dispatcherTimer.Stop();
                    }
                }
                else
                {
                    if (end - gr.Height >= speed)
                    {
                        gr.Height = gr.Height + speed;
                    }
                    else
                    {
                        gr.Height = end;
                    }
                    if (gr.Height >= end)
                    {
                        dispatcherTimer.Stop();
                    }
                }
            };
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(50); //重复间隔
            dispatcherTimer.Start();
        }

       
    }
}
